<template>
    <div v-loading="loading" element-loading-text="登录中..." element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.6)" class="login-container">
    <div class="main">
      <TabLogin/>
      <div class="title-container">
        <h2 class="title">{{getPageTitle('管理端')}}</h2>
      </div>
    <el-form
    ref="ruleFormRef"
    :model="loginForm"
    :rules="loginRules"
    class="demo-ruleForm"
  >
    <el-form-item prop="username">
      <el-input v-model="loginForm.username" type="text" autocomplete="off" :prefix-icon="User" placeholder="username" />
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="loginForm.password" type="password" autocomplete="off" :prefix-icon="Lock"  placeholder="password"/>
    </el-form-item>
    
    <div class="btn">
      <el-button type="primary" @click="submitForm(ruleFormRef)" @keyup.enter="enterClick"
        >登录</el-button
      >
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </div>
  </el-form>
  <div class="tips">
        <span style="margin-right:20px;">如果您还没有管理员账号请先 <span style="color:#409EFF;cursor:pointer" @click="()=>showToast('贤哥:xxxxxxxxx')">联系我贤哥</span></span>
    </div>
    </div>
  </div>
</template>

<script setup>
  import { User, Lock } from '@element-plus/icons-vue'
	import {getPageTitle} from '../../utils/getPageTitle.js'
  import { login } from '../../api/admin.js'
  import { useRouter } from "vue-router";
  import { showToast } from 'vant'
  import { ref,reactive,getCurrentInstance,onMounted,onUpdated } from 'vue'
  import TabLogin from '../../components/admin/TabsLogin.vue'
  const ruleFormRef = ref()
  const { proxy } = getCurrentInstance()
	const _this = proxy
  const router = useRouter();
  const loginForm = reactive({
      username: '',
      password: ''
  })
    
  const loading = ref(false)

  const loginRules = reactive({
      username: [{ required: true, trigger: 'blur', message:'请输入企业用户名',trigger: 'blur' }],
      password: [{ required: true, trigger: 'blur', message:'请输入密码',trigger: 'blur' }]
  })
  const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
      if (valid) {
        login(loginForm).then((res)=>{
          if(res.code === 200){
            localStorage.setItem('admin_token',res.data.token)
            router.push('/admin')
          }
        })
      } else {
        showToast('请输入正确的管理员账号和密码')
        return false
      }
  })
  }
  const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
  }
  const enterClick = (e)=>{
    if (e.keyCode == 13) {
        submitForm(ruleFormRef.value);
      }
  }

  onMounted(()=>{
    window.addEventListener("keydown",enterClick);
  })
  onUpdated(()=>{
    window.removeEventListener('keydown',enterClick,false)
  })
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #333;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.main{position: absolute;left: 50%;top: 50%;transform: translate(-200px,-200px);}
.btn{text-align: center;margin-bottom: 20px;}
.el-input__wrapper{width: 100% !important;}
.login-container .el-input{width: 100% !important;}
.demo-ruleForm{width: 400px; margin: 0 auto !important;}
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      // caret-color: $cursor;
 
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
 
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
 
<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
 
.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  background: url(@/assets/img/login-bg.jpg);
  background-size: 100% 100%;
 
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
 
  }
 
  .tips {
    font-size: 18px;
    text-align: center;
    color: #000;
    margin-bottom: 10px;
  }
 
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
 
  .title-container {
    position: relative;
    margin-top: 20px;
    .title {
      font-size: 30px;
      color: #333;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: 500;
    }
  }
 
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>